//
//  BannerView.m
//  Fangmm
//
//  Created by 阮琦 on 16/2/10.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "BannerView.h"
#import "WebViewController.h"
#import "ACMacros.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/UIButton+WebCache.h>

@implementation BannerView {
    NSInteger pageCount;
    CGFloat bannerWidth;
    CGFloat bannerHeight;
    
    NSMutableArray *imagesArray;
//    NSArray *imageDataUrls;
    NSMutableArray *destinationUrlsArray;
}

- (void)drawRect:(CGRect)rect {
    bannerWidth = rect.size.width;
    bannerHeight = rect.size.height;
    self.bannerScrollView = [[UIScrollView alloc]initWithFrame:rect];
    self.bannerScrollView.delegate = self;
    [self addSubview:self.bannerScrollView];
}

- (void)refreshBannerWithImages:(NSArray *)images {
    imagesArray = images;
    [self refreshBannerWithImagesFromImagesData:true];
}

- (void)refreshBannerWithImages:(NSArray *)images RelatedUrls:(NSArray *)urls {
    imagesArray = images;
    destinationUrlsArray = urls;
    [self refreshBannerWithWebViewsFromImagesData:true];
}

- (void)refreshBannerWithImagesFromImagesData:(BOOL)fromImages{
    for (id item in self.bannerScrollView.subviews) {
        if ([item class] == [UIImageView class] || [item class] == [UIWebView class]) {
            [item removeFromSuperview];
        }
    }
    NSInteger count = imagesArray.count;
    if (count == 0) {
        [self.bannerScrollView setContentSize:CGSizeMake(0, 0)];
        //TestLog
        NSLog(@"banner dataArray is Nil");
    } else {
        [self.bannerScrollView setContentSize:CGSizeMake(count * bannerWidth, 0)];
        for (int i = 0; i < count; i++) {
            UIImageView *bannerPageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * bannerWidth, 0, bannerWidth, bannerHeight)];
            [self.bannerScrollView addSubview:bannerPageView];
            if (fromImages) {
                bannerPageView.image = imagesArray[i];
            } else {
                [bannerPageView sd_setImageWithURL:imagesArray[i]];
            }
            bannerPageView.contentMode = UIViewContentModeScaleAspectFit;
        }
    }
}

- (void)refreshBannerWithWebViewsFromImagesData:(BOOL)fromImages {
    for (id item in self.bannerScrollView.subviews) {
        if ([item class] == [UIImageView class] || [item class] == [UIWebView class]) {
            [item removeFromSuperview];
        }
    }
    NSArray *tempImages = [[NSArray alloc]initWithArray:imagesArray];
    [imagesArray addObject:[tempImages firstObject]];
    [imagesArray insertObject:[tempImages lastObject] atIndex:0];
    NSArray *tempUrls = [[NSArray alloc]initWithArray:destinationUrlsArray];
    [destinationUrlsArray addObject:[tempUrls firstObject]];
    [destinationUrlsArray insertObject:[tempUrls lastObject] atIndex:0];
    NSInteger count = imagesArray.count;
    NSInteger urlCount = destinationUrlsArray.count;
    if (count != urlCount) {
        if (urlCount > count) {
            NSLog(@"import urlstrings are more than images");
        } else {
            NSLog(@"import urlstrings are less than images");
        }
    }
    if (count == 0) {
        [self.bannerScrollView setContentSize:CGSizeMake(0, 0)];
        //TestLog
        NSLog(@"banner dataArray is Nil");
    } else {
        [self.bannerScrollView setContentSize:CGSizeMake(count * bannerWidth, 0)];
        for (int i = 0; i < count; i++) {
            UIButton *bannerPageView = [[UIButton alloc]initWithFrame:CGRectMake(i * bannerWidth, 0, bannerWidth, bannerHeight)];
            [self.bannerScrollView addSubview:bannerPageView];
            if (fromImages) {
                [bannerPageView setImage:imagesArray[i] forState:UIControlStateNormal];
            } else {
                [bannerPageView sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",imagesArray[i]]] forState:UIControlStateNormal];
            }
            bannerPageView.contentMode = UIViewContentModeScaleAspectFill;
            bannerPageView.tag = i;
            if ([destinationUrlsArray[i] isEqual:@""]) {
                
            } else {
                [bannerPageView addTarget:self action:@selector(presentToWebView:) forControlEvents:UIControlEventTouchUpInside];
                [bannerPageView addTarget:self action:@selector(dragAndDoNothing:) forControlEvents:UIControlEventTouchDragInside];
            }
            [self.bannerScrollView setContentOffset:CGPointMake(Main_Screen_Width, 0) animated:false];
        }
    }
}

- (void)refreshBannerWithImageUrls:(NSArray *)urls {
    [self.bannerScrollView setPagingEnabled:true];
    imagesArray = urls;
    [self refreshBannerWithImagesFromImagesData:false];
}

- (void)refreshBannerWithImageUrls:(NSArray *)imageUrls RelatedUrls:(NSArray *)urls {
    [self.bannerScrollView setPagingEnabled:true];
    imagesArray = imageUrls;
    destinationUrlsArray = urls;
    [self refreshBannerWithWebViewsFromImagesData:false];
}

- (void)presentToWebView:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(pageClickedWithDestinationUrl:)]) {
        [self.delegate pageClickedWithDestinationUrl:destinationUrlsArray[sender.tag]];
    }
}

- (void)dragAndDoNothing:(UIButton *)sender {
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == Main_Screen_Width * (imagesArray.count - 1)) {
        [scrollView setContentOffset:CGPointMake(Main_Screen_Width, 0) animated:false];
    } else if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake((imagesArray.count - 2) * Main_Screen_Width, 0) animated:false];
    }
}

@end
